package com.aic.aicdetactor;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class ChartControl {

	private GraphicalView _chart = null;
	private XYSeries _series = null;
	private XYMultipleSeriesDataset _dataset = null;
	private XYMultipleSeriesRenderer _renderer = null;

	public GraphicalView get_chart() {
		return _chart;
	}

	public ChartControl(Context ctx) {
		_series = new XYSeries("");
		_dataset = new XYMultipleSeriesDataset();
		_dataset.addSeries(_series);

		int color = Color.BLUE;
		PointStyle style = PointStyle.CIRCLE;
		_renderer = buildRenderer(color, style, true);
		_renderer.setPanEnabled(false, false); 

		_chart = ChartFactory.getLineChartView(ctx, _dataset, _renderer);

		setChartSettings(null, null, null, Color.YELLOW, Color.BLACK);
	}

	public void changeData(float[] datas) {
		_series.clear();
		float max = datas[0];
		float min = datas[0];
		for (int i = 0; i < datas.length; i++) {
			_series.add(i + 1, datas[i]);
			if (datas[i] > max)
				max = datas[i];
			if (datas[i] < min)
				min = datas[i];
		}
		//setChartSettings(min, max);
		_chart.invalidate();
	}
	
	public void changeData(float[] xdatas,float[] ydatas) {
		_series.clear();
		float max = ydatas[0];
		float min = ydatas[0];
		for (int i = 0; i < xdatas.length; i++) {
			_series.add(xdatas[i], ydatas[i]);
			if (ydatas[i] > max)
				max = ydatas[i];
			if (ydatas[i] < min)
				min = ydatas[i];
		}
		//setChartSettings(min, max);
		_chart.invalidate();
	}

	private XYMultipleSeriesRenderer buildRenderer(int color, PointStyle style, boolean fill) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(color);
		r.setPointStyle(style);
		r.setFillPoints(fill);
		r.setLineWidth(3);
		renderer.addSeriesRenderer(r);

		return renderer;
	}

	public void setChartSettings(String title, String xTitle, String yTitle, int axesColor, int labelsColor) {
		_renderer.setChartTitle(title);
		_renderer.setXTitle(xTitle);
		_renderer.setYTitle(yTitle);
		_renderer.setAxesColor(axesColor);
		_renderer.setLabelsColor(labelsColor);
		_renderer.setShowGrid(true);
		// _renderer.setGridColor(Color.WHITE);
		_renderer.setBackgroundColor(Color.DKGRAY);
		_renderer.setMarginsColor(Color.DKGRAY);
		_renderer.setXLabels(20);
		_renderer.setYLabels(10);
		// renderer.setXTitle("Time");
		// renderer.setYTitle("dBm");
		_renderer.setYLabelsAlign(Align.RIGHT);
		_renderer.setPointSize((float) 2);
		_renderer.setShowLegend(false);
	}

	public void setChartSettings(double xMin, double xMax, double yMin, double yMax) {
		_renderer.setXAxisMin(xMin);
		_renderer.setXAxisMax(xMax);
		_renderer.setYAxisMin(yMin);
		_renderer.setYAxisMax(yMax);
	}

	public void setChartSettings(double yMin, double yMax) {
		_renderer.setYAxisMin(yMin);
		_renderer.setYAxisMax(yMax);
	}
}
